'''
Created on July 05, 2011

@author: yc30rr1
'''
from library.testresults.AbstractReporter import AbstractReporter
from plugins.HTML import Table
from library.AutoLoad import AutoLoad
import os, datetime

class Html(AbstractReporter):
    '''
    classdocs
    '''
    
    tcResults=[]
    

    def __init__(self, setupInfo):
        self.resultLocation=setupInfo[0][1]
        self.createHTMLFile()
        super(self.__class__, self).__init__()
                
    def updateResult(self, instance):
        tcResults1=[]
        #del self.tcResults[:]
        tcResults1.append(instance.tcid)
        tcResults1.append(instance.tcResult)
        tcResults1.append(instance.tcNotes)
        self.tcResults.append(tcResults1)
        htmlString=self.listToHtml(self.tcResults)
        self.writeHTMLFile(str(htmlString))
        
    def listToHtml(self,listValues):
        htmlcode =Table(listValues,header_row=['TESTCASE ID', 'RESULT','TC NOTES'])
        return  htmlcode
    
    def createHTMLFile(self):
        timeStamp = datetime.datetime.now()
        timeStamp1 = str(timeStamp)
        timeStamp2 = timeStamp1.replace(':','_').strip()
        self.resultLocation=self.resultLocation+os.sep+'Results'+timeStamp2+'.html'
        
    def writeHTMLFile(self,htmlString):
        try:
                # This will create a new file or **overwrite an existing file**.
                f = open(self.resultLocation, "w")
                try:
                
                    f.write(htmlString) # Write a sequence of strings to a file
                finally:
                    f.close()
        
        except IOError:
            pass
        return self.resultLocation
    